package com.it.tydic.estate.module.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.it.tydic.estate.common.aop.CodeMsg;
import com.it.tydic.estate.common.aop.Result;
import com.it.tydic.estate.common.cache.DictionaryCache;
import com.it.tydic.estate.common.util.*;
import com.it.tydic.estate.module.mapper.inner.*;
import com.it.tydic.estate.module.model.Dto.TbMoveInDto;
import com.it.tydic.estate.module.model.inner.*;
import com.it.tydic.estate.module.service.MoveInService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: liuhui
 * @createDate: 2020/3/2
 * @version: 1.0
 */
@Service
@Transactional
public class MoveInServiceImpl implements MoveInService {
    @Autowired
    private TbMoveInMapper tbMoveInMapper;  /*迁入*/
    @Autowired
    private TbInformationMapper informationMapper;  /*数据中间表*/
    @Autowired
    private TbCustomerProfileMapper customerProfileMapper;  /*档案*/
    @Autowired
    private FangjianguanliMapper fangjianguanliMapper;      /*房间*/
    @Autowired
    private TbHistoryMapper tbHistoryMapper;    /*历史*/
    /**
     * 客户迁入数据添加(只允许添加没有人收费服务对象的房间)
     * @param tbMoveIn
     * @return
     */
    @Override
    public Result addMoveIn(TbMoveInDto moveIn) {
        /*生成id*/
        InstaIdGenerator idGenerator = new InstaIdGenerator();
        long id = idGenerator.nextId(TimeUtil.timeGen());
        moveIn.setUid(String.valueOf(id));
        moveIn.setOperator(CurrentUtil.getCurrentUserId());
        moveIn.setZfbj(ZfbjFinalUtil.zfbj_zy);
        moveIn.setUpdateTime(new Date());
        /*添加迁入信息*/
        tbMoveInMapper.insertMoveIn(moveIn);
        /*修改房间状态*/
        Fangjianguanli fangjianguanli = new Fangjianguanli();
        fangjianguanli.setId(moveIn.getRoomId());
        fangjianguanli.setFjzt(moveIn.getType());
        fangjianguanli.setSffwdx(moveIn.getPaidobject());
        fangjianguanli.setXgsj(new Date());
        fangjianguanli.setUuserId(CurrentUtil.getCurrentUserId());
        fangjianguanliMapper.updateByPrimaryKeySelective(fangjianguanli);
        /*添加档案信息*/
        TbCustomerProfile tbCustomerProfile = new TbCustomerProfile();
        tbCustomerProfile.setRoomNum(moveIn.getRoomNum());
        int num = customerProfileMapper.selectCount(tbCustomerProfile);
        tbCustomerProfile.setRoomId(moveIn.getRoomId());
        tbCustomerProfile.setMoveId(String.valueOf(id));
        tbCustomerProfile.setUserCode(moveIn.getUserCode());
        long CustId = idGenerator.nextId(TimeUtil.timeGen());
        tbCustomerProfile.setUid(String.valueOf(CustId));
        /*客户编号*/
        tbCustomerProfile.setUserNum(moveIn.getRoomNum()+"1_1"+num);
        /*创建时间，创建人*/
        tbCustomerProfile.setCjsj(new Date());
        tbCustomerProfile.setCuserId(CurrentUtil.getCurrentUserId());
       int resultNum = customerProfileMapper.insertSelective(tbCustomerProfile);
        /*为数据表添加数据*/
        TbInformation tbInformation = new TbInformation();
        tbInformation.setRoomId(moveIn.getRoomId());
        tbInformation.setUid(String.valueOf(idGenerator.nextId(TimeUtil.timeGen())));
        tbInformation.setProfileId(String.valueOf(CustId));
        tbInformation.setMoveId(String.valueOf(id));
        tbInformation.setCuserId(CurrentUtil.getCurrentUserId());
        tbInformation.setCjsj(new Date());
        informationMapper.insertSelective(tbInformation);
        /*添加历史数据*/
        TbHistory tbHistory = new TbHistory();
        tbHistory.setUid(String.valueOf(idGenerator.nextId(TimeUtil.timeGen())));
        tbHistory.setFjid(moveIn.getRoomId());
        tbHistory.setMoveid(String.valueOf(id));
        tbHistory.setNewdate(new Date());
        tbHistory.setUpdateTime(new Date());
        tbHistory.setProfileid(String.valueOf(CustId));
        tbHistory.setType(ZfbjFinalUtil.zfbj_zy);   /*迁入*/
        tbHistory.setZfbj(ZfbjFinalUtil.zfbj_zy);
        tbHistory.setOperator(CurrentUtil.getCurrentUserId());
        tbHistory.setBz("用户迁入历史记录");
        tbHistoryMapper.insertSelective(tbHistory);
        return resultNum>0 ?Result.success(moveIn.getUid()):Result.error(CodeMsg.INSERT_ERROR);
    }

    /**
     * 根据    接口查询列表
     * @param moveIn
     * @return
     */
    @Override
    public Result getList(Map<String,Object> moveIn) {
        /*返回集合*/
        Map<String,Object> map = new HashMap<>();
        /*分页*/
        CommonUtils.pageProcess(moveIn);//验证处理分页
        Page<TbMoveIn> tbMoveIns = tbMoveInMapper.selectByLike(moveIn);
        List<TbMoveIn> result = tbMoveIns.getResult();
        if(result.size()>0 && result!= null){
            /*码表赋值*/
            for (TbMoveIn tbMoveIn : result) {
                tbMoveIn.setFjzt(DictionaryCache.getDicVal(tbMoveIn.getFjzt()));
                tbMoveIn.setFjlx(DictionaryCache.getDicVal(tbMoveIn.getFjlx()));
                tbMoveIn.setSffwdx(DictionaryCache.getDicVal(tbMoveIn.getSffwdx()));
                tbMoveIn.setPaidobject(DictionaryCache.getDicVal(tbMoveIn.getPaidobject()));
            }
        }
        map.put("result",result);
        map.put("totle",tbMoveIns.getTotal());
        return Result.success(map);
    }

    /**
     * 迁入详情
     * @param id
     * @return
     */
    @Override
    public Result getById(String id) {
        /*验证参数*/
        if(StringUtils.isNotEmpty(id)){
            Map<String,Object> map = new HashMap<>();
            map.put("id",id);
            List<TbMoveIn> result = tbMoveInMapper.selectByLike(map).getResult();
            return Result.success(result);
        }
        return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
    }
}
